using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;

// #if UNITY_EDITOR
// using UnityEditor;
// #endif

public class SampleKeyPad : MonoBehaviour
{
    public string currStr;
    public UnityEvent<string> onKeyInput;

    public float timeCounter = 0;
    public float timeInterval = 0.5f;

    public void OnBtnInput(int nmub)
    {
        if (CanInput() == false)
        {
            return;
        }
        currStr += nmub.ToString();
        onKeyInput?.Invoke(currStr);
    }

    public bool CanInput()
    {
        if ((Time.time - timeCounter) < timeInterval)
        {
            return false;
        }
        timeCounter = Time.time;
        return true;
    }

    public void OnBtnInput(char str)
    {
        if (CanInput() == false)
        {
            return;
        }
        currStr += str.ToString();
        onKeyInput?.Invoke(currStr);
    }

    public void OnBtnInput(string str)
    {
        if (CanInput() == false)
        {
            return;
        }
        currStr += str.ToString();
        onKeyInput?.Invoke(currStr);
    }

    public void Enter()
    {
        gameObject.SetActive(false);
        onKeyInput?.Invoke(currStr);
    }

    public void BackQuote()
    {
        if (string.IsNullOrEmpty(currStr))
        {
            return;
        }
        currStr = currStr.Remove(currStr.Length - 1);
        onKeyInput?.Invoke(currStr);
    }
}

// #if UNITY_EDITOR
// [CustomEditor(typeof(SampleKeyPad))]
// public class SampleKeyPadEditor : Editor
// {
//     public override void OnInspectorGUI()
//     {
//         base.OnInspectorGUI();
//         SampleKeyPad keyPad = target as SampleKeyPad;
//         if (GUILayout.Button("打开按钮磁吸"))
//         {
//             var allBtns = keyPad.GetComponentsInChildren<InteractionButton>();
//             foreach (var btn in allBtns)
//             {
//                 btn.magnetic = true;
//             }
//         }
//         if (GUILayout.Button("关闭按钮磁吸"))
//         {
//             var allBtns = keyPad.GetComponentsInChildren<InteractionButton>();
//             foreach (var btn in allBtns)
//             {
//                 btn.magnetic = false;
//             }
//         }
//     }
// }
// #endif
